package com.tyut.musicdreamer.service.music.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * Web配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Value("${music.storage.local.path:./music-files}")
    private String musicFilesPath;
    
    @Value("${music.storage.local.url-prefix:/music-files}")
    private String musicFilesUrlPrefix;
    
    /**
     * 配置静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 确保目录存在
        File musicDir = new File(musicFilesPath);
        if (!musicDir.exists()) {
            musicDir.mkdirs();
        }
        
        // 添加静态资源映射
        String musicFilesLocation = "file:" + musicFilesPath + File.separator;
        // 确保URL以/结尾
        String urlPattern = musicFilesUrlPrefix;
        if (!urlPattern.endsWith("/")) {
            urlPattern += "/";
        }
        registry.addResourceHandler(urlPattern + "**")
                .addResourceLocations(musicFilesLocation);
    }
} 